﻿Option Explicit On
Option Strict On

Imports System.ServiceModel

Namespace Framework.Hardware

    ''' <summary>
    ''' ACOM2000A State Manager
    ''' </summary>
    ''' <remarks></remarks>
    <ServiceBehavior(InstanceContextMode:=ServiceModel.InstanceContextMode.Single)> _
    Public Class StateManager

#Region " Private Members "

        Private m_AmplifierState As New AmplifierState 'Stores the State information for the ACOM2000A
        Private m_UIState As New UIState 'Stores the state information for any UI Elements that may be used

        Private WithEvents m_Connection As ACOM2000Connection

        'Auto Determine Operate Mode
        Private WithEvents m_DetermineOperateModeTimer As New Timers.Timer(1000)
        Private m_DeterminingOperateMode As Boolean
        Private m_DOM_StandbyMessageReceived As Boolean
        Private m_DOM_StandbyLEDOn As Boolean

        Private m_CurrentCOMPort As String = ""
        Private m_DoNotRefresh As Boolean = False
        Private m_SupportsRemotePowerOn As Boolean = False
        Private m_RemotePowerOnMechanism As RemotePowerOnSystem
        Private m_Connecting As Boolean = False

#End Region

#Region " Events "

        Public Event StateChanged As EventHandler
        Public Event ForwardPowerLedChanged As EventHandler(Of PowerLedEventArgs)
        Public Event ReflectedPowerLedChanged As EventHandler(Of PowerLedEventArgs)

#End Region

#Region " Constructor "

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <param name="SupportsPowerOn"></param>
        ''' <param name="DeterminingOperateMode"></param>
        ''' <param name="RemotePowerOnMechanism"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal ComPort As String, ByVal SupportsPowerOn As Boolean, ByVal DeterminingOperateMode As Boolean, ByVal RemotePowerOnMechanism As RemotePowerOnSystem)

            'Validate ComPort isn't nothing 
            If ComPort Is Nothing Then
                Throw New ArgumentNullException("ComPort")
            End If

            m_SupportsRemotePowerOn = SupportsPowerOn
            m_DeterminingOperateMode = DeterminingOperateMode
            m_RemotePowerOnMechanism = RemotePowerOnMechanism
            m_CurrentCOMPort = ComPort.ToUpper

            If m_CurrentCOMPort.Trim = "" Then
                m_AmplifierState.CanConnect = False
            Else
                m_AmplifierState.CanConnect = True
            End If

            m_Connection = New ACOM2000Connection
            m_Connection.DisconnectOnTimeout = False

            RefreshStatus()

        End Sub

#End Region

#Region " Public Properties "

        ''' <summary>
        ''' The Current State of the ACOM2000A
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property State As AmplifierState
            Get
                Return m_AmplifierState
            End Get
        End Property

        ''' <summary>
        ''' The Current COM port being used to connect to the ACOM2000A
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property COMPort As String
            Get
                Return m_CurrentCOMPort
            End Get
            Set(ByVal value As String)
                If value Is Nothing Then
                    Throw New ArgumentNullException("value")
                End If

                'If the connection is open then close it and reset any state
                If Not value.ToUpper = m_CurrentCOMPort Then
                    Dim WasConnected As Boolean = False
                    If m_Connection.IsConnected Then
                        m_Connection.Disconnect()
                        m_AmplifierState = New AmplifierState
                        m_UIState = New UIState
                        WasConnected = True
                        RaiseEvent StateChanged(Me, New EventArgs)
                    End If

                    m_CurrentCOMPort = value

                    If m_CurrentCOMPort.Trim = "" Then
                        m_AmplifierState.CanConnect = False
                    Else
                        m_AmplifierState.CanConnect = True
                    End If
                    RefreshStatus()

                    If WasConnected AndAlso Not m_CurrentCOMPort = "" Then
                        Connect()
                    End If
                End If
            End Set
        End Property

        ''' <summary>
        ''' True if Remote Power on is supported
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SupportsRemotePowerOn As Boolean
            Get
                Return m_SupportsRemotePowerOn
            End Get
            Set(ByVal value As Boolean)
                m_SupportsRemotePowerOn = value
            End Set
        End Property

        ''' <summary>
        ''' The Mechanism used for turning on the Amplifier
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RemotePowerOnMechanism As RemotePowerOnSystem
            Get
                Return m_RemotePowerOnMechanism
            End Get
            Set(ByVal value As RemotePowerOnSystem)
                m_RemotePowerOnMechanism = value
            End Set
        End Property

        ''' <summary>
        ''' The Current UI State
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property UIState As UIState
            Get
                Return m_UIState
            End Get
        End Property

        ''' <summary>
        ''' Is the COM Port Open?
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property COMPortOpen As Boolean
            Get
                Return m_Connection.IsConnected
            End Get
        End Property

#End Region

#Region " ACOM Events "

        Private Sub m_Connection_ACOM2000ADetected(ByVal sender As Object, ByVal e As ACOM2000ADetectedEventArgs) Handles m_Connection.ACOM2000ADetected
            'We have a connection so power must be on
            m_AmplifierState.PowerLED = LedStatus.On
            m_AmplifierState.PowerState = PowerState.Connected

            'Get some initial info from the ACOM2000A
            m_Connection.GetCurrentFrequencySegmentInfo()

            If m_DeterminingOperateMode Then
                DetermineOperateMode()
            End If
        End Sub

        Private Sub m_Connection_ConnectionClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.ConnectionClosed
            m_AmplifierState.PowerState = PowerState.Disconnected
            RefreshStatus()
        End Sub

        Private Sub m_Connection_AirTemperatureChanged(ByVal sender As Object, ByVal e As AirTemperatureEventArgs) Handles m_Connection.AirTemperatureChanged
            m_AmplifierState.CurrentAirTemp = e.Bars
            RefreshStatus()
        End Sub

        Private Sub m_Connection_RCUIsMeasuring(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.RCUIsMeasuring
            If m_AmplifierState.IsMeasuring = True Then
                m_AmplifierState.IsMeasuring = False
            End If
        End Sub

        Private Sub m_Connection_MeasurementMetricUpdated(ByVal sender As Object, ByVal e As MeasurementMetricUpdatedEventArgs) Handles m_Connection.MeasurementMetricUpdated
            If m_AmplifierState.MetricA = e.Metric Then
                m_AmplifierState.MetricAValue = e.Value
            End If
            If m_AmplifierState.MetricB = e.Metric Then
                m_AmplifierState.MetricBValue = e.Value
            End If
        End Sub

        Private Sub m_Connection_LEDStatusUpdate(ByVal sender As Object, ByVal e As LEDStatusEventArgs) Handles m_Connection.LEDStatusUpdate
            m_DoNotRefresh = True

            m_AmplifierState.OperateLED = e.OperateLED
            m_AmplifierState.StandByLED = e.StandByLED
            m_AmplifierState.PlateLED = e.PlateLED
            m_AmplifierState.DriveLED = e.DriveLED
            m_AmplifierState.HeaterLED = e.HeaterLED
            m_AmplifierState.ProtectionLED = e.ProtectionLED

            If m_DeterminingOperateMode Then
                If e.StandByLED = LedStatus.On Then
                    m_DOM_StandbyLEDOn = True
                End If
            End If

            m_DoNotRefresh = False
            RefreshStatus()
        End Sub

        Private Sub m_Connection_PowerOffComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.PowerOffComplete
            If SupportsRemotePowerOn Then
                'If remote power on is supported then set state as powered off
                m_AmplifierState.PowerState = PowerState.PoweredOff
            Else
                'If Remote power on is not supported then disconnect the COM port and set the state accordingly 
                m_Connection.Disconnect()
                m_AmplifierState.PowerState = PowerState.Disconnected
            End If
            RefreshStatus()
        End Sub

        Private Sub m_Connection_PowerOffCoolingTubes(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.PowerOffCoolingTubes
            m_AmplifierState.PowerState = PowerState.PoweringOffCoolingTubes
            RefreshStatus()
        End Sub

        Private Sub m_Connection_PowerOffExecuted(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.PowerOffExecuted
            m_AmplifierState.PowerState = PowerState.PoweringOff
            RefreshStatus()
        End Sub

        Private Sub m_Connection_PowerOnExecuted(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.PowerOnExecuted
            m_AmplifierState.PowerState = PowerState.PoweringOn
            RefreshStatus()
        End Sub

        Private Sub m_Connection_HVOnExecuted(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.HVOnExecuted
            'HV is now ready, so treat the ACOM as on and connected
            m_AmplifierState.PowerState = PowerState.Connected
            RefreshStatus()
        End Sub

        Private Sub m_Connection_ConnectionTimeout(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.ConnectionTimeout
            'We have timed out trying to detect an ACOM2000
            If SupportsRemotePowerOn Then
                'If we support remote power on then assume it's turned off
                m_AmplifierState.PowerState = PowerState.PoweredOff
            Else
                'If remote power on is not supported then assume we are disconnected
                m_AmplifierState.PowerState = PowerState.Disconnected
                m_Connection.Disconnect()
            End If
            RefreshStatus()
        End Sub

        Private Sub m_Connection_OperateModeOn(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.OperateModeOn
            m_AmplifierState.OperateMode = OperateMode.Operate
            RefreshStatus()
        End Sub

        Private Sub m_Connection_StandByModeOn(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Connection.StandByModeOn
            If m_DeterminingOperateMode Then
                m_DOM_StandbyMessageReceived = True
            End If

            m_AmplifierState.OperateMode = OperateMode.Standby
            RefreshStatus()
        End Sub

        Private Sub m_Connection_BandFrequencyChanged(ByVal sender As Object, ByVal e As BandFrequencyEventArgs) Handles m_Connection.BandFrequencyChanged
            m_AmplifierState.Band = e.Band
            m_AmplifierState.Segment = e.Segment
            m_AmplifierState.USRMode = e.USRMode
            m_AmplifierState.AntennaNumber = e.AntennaNumber
            RefreshStatus()
        End Sub

        Private Sub m_Connection_ReflectedPowerLedUpdated(ByVal sender As Object, ByVal e As PowerLedEventArgs) Handles m_Connection.ReflectedPowerLedUpdated
            RaiseEvent ReflectedPowerLedChanged(Me, e)
        End Sub

        Private Sub m_Connection_ForwardPowerLedUpdated(ByVal sender As Object, ByVal e As PowerLedEventArgs) Handles m_Connection.ForwardPowerLedUpdated
            RaiseEvent ForwardPowerLedChanged(Me, e)
        End Sub

        Private Sub m_Connection_AmplifierError(ByVal sender As Object, ByVal e As ErrorEventArgs) Handles m_Connection.AmplifierError
            m_AmplifierState.PowerStatePriorToError = m_AmplifierState.PowerState

            Dim ErrorText() As String = e.Message.Split("|"c)
            m_AmplifierState.CurrentErrorMessageLine1 = ErrorText(0).Replace("$var$", e.ParameterValue)
            If ErrorText.Length = 2 Then
                m_AmplifierState.CurrentErrorMessageLine2 = ErrorText(1).Replace("$var$", e.ParameterValue)
            Else
                m_AmplifierState.CurrentErrorMessageLine2 = ""
            End If

            If m_AmplifierState.CurrentErrorMessageLine1.Length < 20 Then
                Dim Spaces As Integer = 20 - m_AmplifierState.CurrentErrorMessageLine1.Length
                If Spaces Mod 2 = 0 Then
                    m_AmplifierState.CurrentErrorMessageLine1 = StrDup(Spaces \ 2, " ") & m_AmplifierState.CurrentErrorMessageLine1 & StrDup(Spaces \ 2, " ")
                Else
                    Spaces = Spaces - 1
                    m_AmplifierState.CurrentErrorMessageLine1 = StrDup(Spaces \ 2, " ") & m_AmplifierState.CurrentErrorMessageLine1 & StrDup(Spaces \ 2, " ") & " "
                End If
            End If
            If m_AmplifierState.CurrentErrorMessageLine2.Length < 20 Then
                Dim Spaces As Integer = 20 - m_AmplifierState.CurrentErrorMessageLine2.Length
                If Spaces Mod 2 = 0 Then
                    m_AmplifierState.CurrentErrorMessageLine2 = StrDup(Spaces \ 2, " ") & m_AmplifierState.CurrentErrorMessageLine2 & StrDup(Spaces \ 2, " ")
                Else
                    Spaces = Spaces - 1
                    m_AmplifierState.CurrentErrorMessageLine2 = StrDup(Spaces \ 2, " ") & m_AmplifierState.CurrentErrorMessageLine2 & StrDup(Spaces \ 2, " ") & " "
                End If
            End If

            m_AmplifierState.PowerState = PowerState.ErrorState
        End Sub

#End Region

#Region " Public Methods "

        Public Sub OperateStandby()
            If m_AmplifierState.PowerState = PowerState.ErrorState Then
                m_AmplifierState.PowerState = m_AmplifierState.PowerStatePriorToError
            End If

            If m_AmplifierState.OperateMode = OperateMode.Standby Then
                m_Connection.EnterOperateMode()
            Else
                m_Connection.EnterStandbyMode()
            End If
        End Sub

        Public Sub OnOff()
            'If the power is already on then turn it off?
            If m_AmplifierState.PowerState = PowerState.Connected Then
                m_Connection.TurnAmplifierOff()
            ElseIf m_AmplifierState.PowerState = PowerState.PoweredOff Then
                If SupportsRemotePowerOn Then
                    m_Connection.PerformRemotePowerOn(m_RemotePowerOnMechanism)
                End If
            End If
        End Sub

        Public Sub SelectAntenna0()
            m_Connection.SelectAntenna(0)
        End Sub

        Public Sub SelectAntenna1()
            m_Connection.SelectAntenna(1)
        End Sub

        Public Sub SelectAntenna2()
            m_Connection.SelectAntenna(2)
        End Sub

        Public Sub SelectAntenna3()
            m_Connection.SelectAntenna(3)
        End Sub

        Public Sub SelectAntenna4()
            m_Connection.SelectAntenna(4)
        End Sub

        Public Sub SelectAntenna5()
            m_Connection.SelectAntenna(5)
        End Sub

        Public Sub SelectAntenna6()
            m_Connection.SelectAntenna(6)
        End Sub

        Public Sub SelectAntenna7()
            m_Connection.SelectAntenna(7)
        End Sub

        Public Sub SelectAntenna8()
            m_Connection.SelectAntenna(8)
        End Sub

        Public Sub SelectAntenna9()
            m_Connection.SelectAntenna(9)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Connect()
            m_Connecting = True
            m_UIState.ConnectEnabled = False
            m_UIState.DisconnectEnabled = True
            RefreshStatus()
            If m_Connection.IsConnected Then
                m_Connection.Disconnect()
            End If
            m_AmplifierState.PowerState = PowerState.Connecting
            m_Connecting = False
            m_Connection.Connect(m_CurrentCOMPort)
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Disconnect()
            m_Connection.Disconnect()
            m_UIState.ConnectEnabled = True
            m_UIState.DisconnectEnabled = False
            m_Connection.Disconnect()
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="MetricA"></param>
        ''' <param name="MetricB"></param>
        ''' <remarks></remarks>
        Public Sub StartMeasuring(ByVal MetricA As MeasurementMetricType, ByVal MetricB As MeasurementMetricType)

            m_AmplifierState.MetricA = MetricA
            m_AmplifierState.MetricB = MetricB
            m_Connection.StartMeasurement(m_AmplifierState.MetricA, m_AmplifierState.MetricB)

            m_UIState.StopMeasuringEnabled = True
            m_UIState.StartMeasuringEnabled = False
            m_AmplifierState.IsMeasuring = True

        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub SetFrequencyBand(ByVal FrequencyHertz As Integer)
            If m_Connection.IsConnected Then
                m_Connection.SetFrequencyBand(FrequencyHertz)
            End If
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub StopMeasuring()
            m_Connection.StopMeasuring()
            m_UIState.StopMeasuringEnabled = False
            m_UIState.StartMeasuringEnabled = True
            m_AmplifierState.IsMeasuring = False
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="Metric"></param>
        ''' <remarks></remarks>
        Public Sub SetMetricB(ByVal Metric As MeasurementMetricType)
            m_AmplifierState.MetricB = Metric
            If m_AmplifierState.IsMeasuring Then
                m_Connection.StartMeasurement(m_AmplifierState.MetricA, m_AmplifierState.MetricB)
            End If
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="Metric"></param>
        ''' <remarks></remarks>
        Public Sub SetMetricA(ByVal Metric As MeasurementMetricType)
            m_AmplifierState.MetricA = Metric
            If m_AmplifierState.IsMeasuring Then
                m_Connection.StartMeasurement(m_AmplifierState.MetricA, m_AmplifierState.MetricB)
            End If
        End Sub

#End Region

#Region " Private Methods "

        Private Sub RefreshStatus()
            If m_DoNotRefresh Then Exit Sub

            If m_Connecting Then
                m_AmplifierState.DisplayLine1 = "     CONNECTING     "
                m_AmplifierState.DisplayLine2 = "    PLEASE WAIT...  "
                m_AmplifierState.DisplayLine3 = "                    "
                m_AmplifierState.DisplayLine4 = "                    "

                'Turn off all LED's
                m_AmplifierState.PowerLED = LedStatus.Off
                m_AmplifierState.OperateLED = LedStatus.Off
                m_AmplifierState.StandByLED = LedStatus.Off
                m_AmplifierState.PlateLED = LedStatus.Off
                m_AmplifierState.DriveLED = LedStatus.Off
                m_AmplifierState.HeaterLED = LedStatus.Off
                m_AmplifierState.ProtectionLED = LedStatus.Off

                m_UIState.OperateStandbyEnabled = False
                m_UIState.OnOffEnabled = False
                m_UIState.Antenna0Enabled = False
                m_UIState.Antenna1Enabled = False
                m_UIState.Antenna2Enabled = False
                m_UIState.Antenna3Enabled = False
                m_UIState.Antenna4Enabled = False
                m_UIState.Antenna5Enabled = False
                m_UIState.Antenna6Enabled = False
                m_UIState.Antenna7Enabled = False
                m_UIState.Antenna8Enabled = False
                m_UIState.Antenna9Enabled = False
                m_UIState.StartMeasuringEnabled = False
                m_UIState.StopMeasuringEnabled = False
                m_UIState.MetricASelectionEnabled = False
                m_UIState.MetricBSelectionEnabled = False
                m_UIState.DisconnectEnabled = True
                m_UIState.ConnectEnabled = False

            Else

                Select Case m_AmplifierState.PowerState
                    Case PowerState.Disconnected
                        m_AmplifierState.DisplayLine1 = "    DISCONNECTED    "
                        m_AmplifierState.DisplayLine2 = "                    "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        'Turn off all LED's
                        m_AmplifierState.PowerLED = LedStatus.Off
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        m_UIState.OnOffEnabled = False
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False
                        m_UIState.DisconnectEnabled = False

                        If m_Connecting Or Not m_AmplifierState.CanConnect Then
                            m_UIState.ConnectEnabled = False
                        Else
                            m_UIState.ConnectEnabled = True
                        End If


                    Case PowerState.Connecting
                        m_AmplifierState.DisplayLine1 = "     CONNECTING     "
                        m_AmplifierState.DisplayLine2 = "    PLEASE WAIT...  "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        'Turn off all LED's
                        m_AmplifierState.PowerLED = LedStatus.Off
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        m_UIState.OnOffEnabled = False
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False
                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.PoweredOff
                        m_AmplifierState.DisplayLine1 = "    POWER IS OFF    "
                        m_AmplifierState.DisplayLine2 = "                    "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        'Turn off all LED's
                        m_AmplifierState.PowerLED = LedStatus.Off
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        If SupportsRemotePowerOn Then
                            m_UIState.OnOffEnabled = True
                        Else
                            m_UIState.OnOffEnabled = False
                        End If
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.PoweringOff
                        m_AmplifierState.DisplayLine1 = "    POWERING OFF    "
                        m_AmplifierState.DisplayLine2 = "    PLEASE WAIT...  "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        m_AmplifierState.PowerLED = LedStatus.On
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        m_UIState.OnOffEnabled = False
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.PoweringOffCoolingTubes
                        m_AmplifierState.DisplayLine1 = "    COOLING TUBES   "
                        m_AmplifierState.DisplayLine2 = "    PLEASE WAIT...  "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        m_AmplifierState.PowerLED = LedStatus.On
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        m_UIState.OnOffEnabled = False
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.PoweringOn
                        m_AmplifierState.DisplayLine1 = "POWER ON IN PROGRESS"
                        m_AmplifierState.DisplayLine2 = "    PLEASE WAIT...  "
                        m_AmplifierState.DisplayLine3 = "                    "
                        m_AmplifierState.DisplayLine4 = "                    "

                        m_AmplifierState.PowerLED = LedStatus.On
                        m_AmplifierState.OperateLED = LedStatus.Off
                        m_AmplifierState.StandbyLED = LedStatus.Off
                        m_AmplifierState.PlateLED = LedStatus.Off
                        m_AmplifierState.DriveLED = LedStatus.Off
                        m_AmplifierState.HeaterLED = LedStatus.Off
                        m_AmplifierState.ProtectionLED = LedStatus.Off

                        m_UIState.OperateStandbyEnabled = False
                        m_UIState.OnOffEnabled = False
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.ErrorState
                        m_AmplifierState.DisplayLine1 = m_AmplifierState.CurrentErrorMessageLine1
                        m_AmplifierState.DisplayLine2 = m_AmplifierState.CurrentErrorMessageLine2

                        m_UIState.OperateStandbyEnabled = True
                        m_UIState.OnOffEnabled = True
                        m_UIState.Antenna0Enabled = False
                        m_UIState.Antenna1Enabled = False
                        m_UIState.Antenna2Enabled = False
                        m_UIState.Antenna3Enabled = False
                        m_UIState.Antenna4Enabled = False
                        m_UIState.Antenna5Enabled = False
                        m_UIState.Antenna6Enabled = False
                        m_UIState.Antenna7Enabled = False
                        m_UIState.Antenna8Enabled = False
                        m_UIState.Antenna9Enabled = False
                        m_UIState.StartMeasuringEnabled = False
                        m_UIState.StopMeasuringEnabled = False
                        m_UIState.MetricASelectionEnabled = False
                        m_UIState.MetricBSelectionEnabled = False

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                    Case PowerState.Connected

                        m_UIState.OnOffEnabled = True
                        m_UIState.OperateStandbyEnabled = True
                        m_UIState.Antenna0Enabled = True
                        m_UIState.Antenna1Enabled = True
                        m_UIState.Antenna2Enabled = True
                        m_UIState.Antenna3Enabled = True
                        m_UIState.Antenna4Enabled = True
                        m_UIState.Antenna5Enabled = True
                        m_UIState.Antenna6Enabled = True
                        m_UIState.Antenna7Enabled = True
                        m_UIState.Antenna8Enabled = True
                        m_UIState.Antenna9Enabled = True

                        m_UIState.DisconnectEnabled = True
                        m_UIState.ConnectEnabled = False

                        Dim TopLine As String = "£C" & StrDup(m_AmplifierState.CurrentAirTemp, "¬"c) & StrDup(13 - m_AmplifierState.CurrentAirTemp, "_"c) & " "
                        If m_AmplifierState.OperateMode = OperateMode.Standby Then
                            TopLine &= "STBY"
                        Else
                            TopLine &= "OPER"
                        End If
                        m_AmplifierState.DisplayLine1 = TopLine

                        Dim Line2 As String
                        If m_AmplifierState.Segment IsNot Nothing Then
                            Line2 = (m_AmplifierState.Segment.LowerFrequency / 1000).ToString("00.000") & "-" & (m_AmplifierState.Segment.UpperFrequency / 1000).ToString("00.000") & " "
                        Else
                            Line2 = "UNKNOWN SEG   "
                        End If

                        If m_AmplifierState.AntennaNumber = 0 Then
                            Line2 &= "AM "
                        Else
                            Line2 &= "A" & m_AmplifierState.AntennaNumber.ToString() & " "
                        End If

                        If m_AmplifierState.USRMode Then
                            Line2 &= "USR"
                        Else
                            Line2 &= "DEF"
                        End If
                        m_AmplifierState.DisplayLine2 = Line2


                        Dim MeasurementTitleLine As String = "                    "
                        Dim MeasurementValueLine As String = "                    "
                        If m_AmplifierState.IsMeasuring Then
                            m_UIState.StartMeasuringEnabled = False
                            m_UIState.StopMeasuringEnabled = True

                            MeasurementTitleLine = " " & ACOM2000Connection.GetMeasurementTitle(m_AmplifierState.MetricA) & "  " & ACOM2000Connection.GetMeasurementTitle(m_AmplifierState.MetricB) & " "
                            MeasurementValueLine = " " & m_AmplifierState.MetricAValue & StrDup(8 - m_AmplifierState.MetricAValue.Length, " "c) & "  " & m_AmplifierState.MetricBValue & StrDup(8 - m_AmplifierState.MetricBValue.Length, " "c) & " "
                        Else
                            m_UIState.StartMeasuringEnabled = True
                            m_UIState.StopMeasuringEnabled = False
                        End If
                        m_AmplifierState.DisplayLine3 = MeasurementTitleLine
                        m_AmplifierState.DisplayLine4 = MeasurementValueLine

                        m_UIState.MetricASelectionEnabled = True
                        m_UIState.MetricBSelectionEnabled = True

                End Select

            End If

            RaiseEvent StateChanged(Me, New EventArgs)

        End Sub

        Private Sub DetermineOperateMode()
            m_DeterminingOperateMode = True
            m_DOM_StandbyLEDOn = False
            m_DOM_StandbyMessageReceived = False

            m_Connection.EnterStandbyMode()
            m_DetermineOperateModeTimer.Start()
        End Sub

        Private Sub m_DetermineOperateModeTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_DetermineOperateModeTimer.Elapsed
            m_DetermineOperateModeTimer.Stop()
            m_DeterminingOperateMode = False

            If m_DOM_StandbyLEDOn And m_DOM_StandbyMessageReceived Then
                'Mode was operate
                m_Connection.EnterOperateMode()
            Else
                'Mode was standby
                m_AmplifierState.StandByLED = LedStatus.On
            End If
        End Sub

#End Region

    End Class

End Namespace